#include <Reg51.h>
#include "delay.h"
#include "LCD1602.h"
#include "Key.h"
#include "menu.h"

void KeyService();
void LCDShowService();
void Timer0Init(void);

int TEMPbuf[] = { 125,126,124,126,223,224,225,226,335,336,337,338,441,442,443,444 };
unsigned char menu;
bit mode = 0;
unsigned int timer0Cnt;
unsigned char menuFlag;

void main()
{
	LCDInit();
	Timer0Init();
	DelayXms(10);
	StartMenu();
	DelayXms(2000);
	LCDWriteCmd(LCD_SCREEN_CLR);
	while(1)
	{
		if (0 == mode)
		{
			TR0 = 1;
			if (1 == menuFlag)
			{
				menuFlag = 0;
				menu++;
				if (4 == menu)
					menu = 0;
			}
		}
		KeyService();
		LCDShowService();
	}
}

void KeyService()
{
	switch(Key_Scan())
	{
		case 1:mode = ~mode; break;
		case 2:TR0 = 0;
				if(mode == 1)
				{
					menu++;
					if(menu == 4) 
						menu = 0;
					LCDWriteCmd(LCD_SCREEN_CLR);
				}
				break;
		case 3:mode = 1; TR0 = 0;menu = 0;LCDWriteCmd(LCD_SCREEN_CLR);break;
		case 4:menu = 4;LCDWriteCmd(LCD_SCREEN_CLR);break;
		default:break;
	}
}

void LCDShowService()
{
	switch(menu)
	{
		case 0:SubmenuOne();break;
		case 1:SubmenuTwo();break;
		case 2:SubmenuThree();break;
		case 3:SubmenuFour();break;
		case 4:AlarmSetMenu();break;
		default:break;
	}
}

void Timer0Init(void)		//1毫秒@11.0592MHz
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x66;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	ET0 = 1;
	EA = 1;
	//TR0 = 1;		//定时器0开始计时
}

void timer0_ISR() interrupt 1
{
	TR0 = 0;
	timer0Cnt++;
	if (timer0Cnt >= 2000)
	{
		timer0Cnt = 0;
		menuFlag = 1;
	}
	TL0 = 0x66;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TR0 = 1;
}

